package com.gjt.zyglxt.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Expression;
import com.gjt.zyglxt.dao.IMemberDao;
import com.gjt.zyglxt.dto.MemberDTO;
import com.gjt.zyglxt.model.Member;
import com.gjt.zyglxt.unti.CardUtil;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * MemberServiceImp
 */
@Service
public class MemberServiceImp implements IMemberService {

    @Resource
    private IMemberDao memberdao;

    @Override
    public Page<Member> findPage(MemberDTO dto) {
        Sort sort = Sort.by(Sort.Order.asc("id"));
         Pageable page = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), sort); 
         return memberdao.findAll(getWhereClause(dto), page); 
     
    }

    

    private Specification<Member> getWhereClause(final MemberDTO dto) {
        return new Specification<Member>() {
            /**
             *
             */
            private static final long serialVersionUID = 1L;

            public javax.persistence.criteria.Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
               Predicate p = criteriaBuilder.conjunction();//动态SQL查询
               List<Expression<Boolean>> exList = p.getExpressions();
               if (dto.getCardNo()!=null && !"".equals(dto.getCardNo())) {
                exList.add(criteriaBuilder.equal(root.<String>get("cardNo"),dto.getCardNo()));
               }
               if (dto.getName()!=null && !"".equals(dto.getName())) {
                   exList.add(criteriaBuilder.like(root.<String>get("name"), "%"+dto.getName()+"%"));   
               }
               if (dto.getPhone()!=null && !"".equals(dto.getPhone())) {
                   exList.add(criteriaBuilder.equal(root.<String>get("phone"), dto.getPhone()));
               }
               if (dto.getStartDate()!=null) {
                   exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"), dto.getStartDate()));
               }
               if (dto.getEndDate()!=null) {
                   exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("regdate"), dto.getEndDate()));
               }
               if (dto.getStatus()!=null) {
                   exList.add(criteriaBuilder.equal(root.<String>get("status"), dto.getStatus()));
               }
               if (dto.getCategory()!=null && !"".equals(dto.getCategory())){
                   if (dto.getDepartment()!=null && !"".equals(dto.getDepartment())) {
                       exList.add(criteriaBuilder.equal(root.<String>get("department"), dto.getDepartment()));
                   }
                   if (dto.getProfession()!=null && !"".equals(dto.getProfession())) {
                       exList.add(criteriaBuilder.equal(root.<String>get("profession"), dto.getProfession()));
                   }
                   if (dto.getGrade()!=null && !"".equals(dto.getGrade())) {
                       exList.add(criteriaBuilder.equal(root.<String>get("grade"), dto.getGrade()));
                   }
               }
               return p;
            }
        };
    }

    @Override
    public Member saveMember(Member m) {
        //将会员卡号设置为时间戳
        m.setCardNo(CardUtil.getCardNo());
        return memberdao.save(m);
    }

    @Override
    public void del(Integer id) {
        // 根据id删除会员信息
        memberdao.deleteById(id);
    }

    @Override
    public int addAll(List<Member> list) {
        // 批量添加
        List<Member> memberList = memberdao.saveAll(list);
        return memberList.size();
    }

    @Override
    public void delAll(List<Member> id) {
        
        memberdao.deleteInBatch(id);
    }

  

    
}